package com.lanou.service.impl;


import com.lanou.entity.*;
import com.lanou.mapper.SearchItemMapper;
import com.lanou.service.SearchService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CommonParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchItemMapper searchItemMapper;
    @Autowired
    private HttpSolrClient.Builder solrClientBuilder;

    @Override
    public List<SearchItem> findAll() throws IOException, SolrServerException {
        HttpSolrClient solrClient = solrClientBuilder.build();
        List<SearchItem> searchItemList = searchItemMapper.findAll();
//        System.out.println("查询所有的数据");
        for (SearchItem searchItem : searchItemList) {
            SolrInputDocument document = new SolrInputDocument();
            Long id = searchItem.getId();
            String title = searchItem.getTitle();
            String sellPoint = searchItem.getSell_point();
            Long price = searchItem.getPrice();
            String image = "//" + searchItem.getImage();
            Long cid = searchItem.getCid();
            String cat = searchItem.getCat();
            String desc = searchItem.getItem_desc();
//            System.out.println(title);
//            System.out.println(desc);

            document.addField("id", id);
            document.addField("item_title", title);
            document.addField("item_sell_point", sellPoint);
            document.addField("item_price", price);
            document.addField("item_image", image);
            document.addField("item_cid", cid);
            document.addField("item_cat", cat);
            document.addField("item_desc", desc);

            solrClient.add(document);
            solrClient.commit();
        }
        solrClient.close();
        return searchItemList;
    }

    @Override
    public SearchItem2 searchByKeyword(String keyword, Integer page, Integer rows,Integer sort) throws IOException, SolrServerException {
        HttpSolrClient solrClient = solrClientBuilder.build();
        SolrQuery query = new SolrQuery(keyword);
        page = (page -1)*rows;
        query.setStart(page).setRows(rows);

        query.set("defType","dismax");
        query.set("qf","item_cat^5 item_title^0.5 item_sell_point^0.1");
        if (sort == 1){
            query.set("sort","score desc,item_price desc");
        }else if (sort == -1){
            query.set("sort","score desc,item_price asc");
        }
        QueryResponse response = solrClient.query(query);
        SolrDocumentList documents = response.getResults();
        for (SolrDocument document : documents) {
            String title = (String)document.getFieldValue("item_title");
            String point = (String)document.getFieldValue("item_sell_point");
            String cat = (String)document.getFieldValue("item_cat");
//            System.out.println(title);
//            System.out.println(cat);
//            System.out.println(point);
        }
        SearchItem2 searchItem2 = new SearchItem2();
        searchItem2.setDocuments(documents);
        double pages = Math.ceil(documents.getNumFound()/rows.doubleValue());
        searchItem2.setPagenums(pages);
        searchItem2.setRownums(documents.getNumFound());
        solrClient.close();
        return searchItem2;
    }

    @Override
    public boolean addItemToSolr(Long id) {
       try {
           HttpSolrClient solrClient = solrClientBuilder.build();
           SearchItem searchItem = searchItemMapper.findById(id);
           SolrInputDocument document = new SolrInputDocument();
           document.addField("id",searchItem.getId());
           document.addField("item_title",searchItem.getTitle());
           document.addField("item_sell_point",searchItem.getSell_point());
           document.addField("item_cat",searchItem.getCat());
           document.addField("item_desc",searchItem.getItem_desc());
           document.addField("item_price",searchItem.getPrice());
           document.addField("item_cid",searchItem.getCid());
           document.addField("item_image",searchItem.getImage());
           solrClient.add(document);
           solrClient.commit();
           solrClient.close();
           return true;
       }catch (Exception e){
           return false;
       }
    }

    @Override
    public boolean delItemToSolr(Long id) {
        try {
            HttpSolrClient solrClient = solrClientBuilder.build();
            solrClient.deleteById(String.valueOf(id));
            solrClient.commit();
            solrClient.close();
            return true;
        }catch (Exception e){
            return false;
        }
    }
}

